﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FloodFree.Common;

namespace FloodFree.Web.Controllers
{
    public class SubscriptionsController : BaseController
    {
        //
        // GET: /Subscriptions/

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        [Authorize]
        public JsonResult Create()
        {
            
            String sensorId = Request.Form["SensorId"];
            Subscription sub = new Subscription();
            sub.SensorId = Int32.Parse(sensorId);
            sub.StatusType = 1;
            sub.Username = HttpContext.User.Identity.Name;
            db.Subscriptions.Add(sub);
            db.SaveChanges();
            return Json(new { Message = "Incrição criada" });
        }

        [HttpDelete]
        [Authorize]
        public JsonResult Delete()
        {
            String subId = Request.Form["SubscriptionId"];
            int subIdint = Int32.Parse(subId);
            Subscription sub = (from Subscription s in db.Subscriptions
                                where s.Id.Equals(subIdint)
                                select s).First();

            db.Subscriptions.Remove(sub);
            db.SaveChanges();
            return Json(new { Message = "Inscricao removida" });
        }

    }
}
